package com.jy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.pojo.CheckGroup;
import com.jy.pojo.CheckItem;
import com.jy.service.CheckGroupService;
import com.jy.utils.MessageConstant;
import com.jy.utils.QueryPageBean;
import com.jy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkgroup")
public class CheckGroupController {
    @Autowired
    CheckGroupService checkGroupService;


    //如果路径中,传过来的是基本数据类型,直接接
    //如果路径中
    @RequestMapping("add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        }catch (Exception e){
            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return Result.ok(MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findPage")
    public Page findPage(@RequestBody QueryPageBean qb){
        return checkGroupService.findPage(qb);
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return Result.ok(checkGroup);
        } catch (Exception e) {
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return Result.ok(checkItemIds);
        }catch (Exception e){
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody CheckGroup checkGroup,@RequestParam List<Integer> checkitemIds) {
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
        } catch (Exception e) {
            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return Result.ok(MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            checkGroupService.delete(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
